package com.pur.zsms.company.web;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONObject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.pur.zsms.common.exception.GenericException;
import com.pur.zsms.common.support.PageController;
import com.pur.zsms.common.support.PaginationSupport;
import com.pur.zsms.company.service.ICompanyService;
import com.pur.zsms.company.vo.CompanyVo;
import com.pur.zsms.matermanage.mater.vo.MaterVo;
import com.pur.zsms.pmsnmanage.role.vo.RoleVo;
import com.pur.zsms.pmsnmanage.user.po.UserPo;
import com.pur.zsms.pmsnmanage.user.vo.UserVo;
import com.pur.zsms.supplier.service.ISupplierService;
import com.pur.zsms.supplier.vo.SupplierVo;

@Controller
@RequestMapping(value = "/companySup")
public class CompanyControllorForSup {

	@SuppressWarnings("unused")
	private static final Logger log = LoggerFactory.getLogger(CompanyControllorForSup.class);

	protected int page = 1;

	protected int pageSize = 15;

	PaginationSupport pageList;

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	@Resource(name = "companyService")
	private ICompanyService companyService;

	@Resource(name = "supplierService")
	private ISupplierService supplierService;

	@RequestMapping(value = "/companyListForSup.do", method = { RequestMethod.POST })
	@ResponseBody
	public String companyListForSup(@RequestParam(required = false) String page, @RequestParam(required = false) String rows, CompanyVo companyVo,
			HttpSession session) throws GenericException {
		JSONObject json = new JSONObject();
		if (page == null || page.equals(""))
			page = "1";
		if (rows == null || rows.equals(""))
			rows = "10";

		UserPo userInfo = (UserPo) session.getAttribute("user");
		int supId = supplierService.getSupIdByUserId(userInfo.getUserId());
		companyVo.setSupId(supId);

		PageController pageController = new PageController(Integer.parseInt(page), Integer.parseInt(rows));
		this.pageList = companyService.listCompanyForSup(pageController, companyVo);
		List<CompanyVo> companyList = new ArrayList<CompanyVo>();
		List items = pageList.getItems();
		if (items != null) {
			for (int i = 0; i < items.size(); i++) {
				Object[] item = (Object[]) items.get(i);
				CompanyVo f = new CompanyVo();
				f.setCompanyId((Integer) item[0]);
				f.setCompanyName((String) item[1]);
				f.setShopFlag((String) item[2]);
				f.setSupFlag((String) item[3]);
				f.setDisFlag((String) item[4]);
				companyList.add(f);
			}
		}
		json.put("success", true);
		json.put("total", pageList.getTotalCount());
		json.put("rows", companyList);
		log.info("----" + json.toString());
		return json.toString();
	}

	@RequestMapping(value = "/goDetailedShopForSup.do", method = { RequestMethod.GET })
	public ModelAndView goDetailedShopForSup(int companyId) {
		Map map = new HashMap();
		map.put("companyId", companyId);
		return new ModelAndView("/shop/shopIndexForSup", map);
	}

	@RequestMapping(value = "/goDetailedSup.do", method = { RequestMethod.GET })
	public ModelAndView goDetailedSup(int companyId) {
		Map map = new HashMap();
		map.put("companyId", companyId);
		return new ModelAndView("/supplier/supplierIndexForCompany", map);
	}

	@RequestMapping(value = "/preSaveCompanyForSup.do", method = { RequestMethod.GET })
	public ModelAndView preSaveCompanyForSup(HttpSession session) {
		JSONObject json = new JSONObject();
		List<RoleVo> roleList = null;
		long companyUser = 0;

		Map map = new HashMap();

		return new ModelAndView("/company/addCompanyForSup", map);
	}

	@RequestMapping(value = "/saveCompanyForSup.do", method = { RequestMethod.POST })
	@ResponseBody
	public String saveCompanyForSup(CompanyVo c, HttpSession session) {
		JSONObject json = new JSONObject();
		UserPo lgUser = (UserPo) session.getAttribute("user");
		try {
			int supId = supplierService.getSupIdByUserId(lgUser.getUserId());
			c.setSupId(supId);
			boolean flag = companyService.saveCompanyForSup(c);
			if (flag) {
				json.put("success", true);
			} else {
				json.put("success", false);
				json.put("msg", companyService.getMsg());
			}
		} catch (Exception e) {
			e.printStackTrace();
			json.put("success", false);
		}
		return json.toString();
	}

}
